package com.zhanggm.s3.house.controller;


import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhanggm.s3.core.utils.AjaxResult;
import com.zhanggm.s3.house.elasticsearch.HouseAnjukeRepository;
import com.zhanggm.s3.house.entity.HouseAnjuke;
import com.zhanggm.s3.house.service.IHouseAnjukeService;
import com.zhanggm.s3.house.webmagic.AnJuKeSpider;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.FilePipeline;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2023-07-28
 */
@Slf4j
@RestController
@RequestMapping("/house-anjuke")
public class HouseAnjukeController {

    @Autowired
    private IHouseAnjukeService houseAnjukeService;

    @Autowired
    private HouseAnjukeRepository houseAnjukeRepository;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;


    @GetMapping("page")
    public AjaxResult page(HouseAnjuke house, Integer pageNum, Integer pageSize) {
        PageInfo pageInfo = houseAnjukeService.getPageInfo(house, pageNum, pageSize);
        return AjaxResult.success(pageInfo);
        /*PageHelper.startPage(pageNum, pageSize);
        List<HouseAnjuke> list = houseAnjukeService.list();
        return AjaxResult.success(new PageInfo<>(list));*/
    }

    /**
     * 安居客房源抓取
     * @return
     */
    @GetMapping("spider")
    public AjaxResult spider() {
        Spider.create(new AnJuKeSpider())
                .addUrl("https://bj.zu.anjuke.com/?t=1&from=0&comm_exist=on&kw=")
                .addPipeline(new Pipeline() {
                    @Override
                    public void process(ResultItems resultItems, Task task) {
                        List<Map<String, Object>> pageData = (List<Map<String, Object>>)resultItems.get("pageData");
                        pageData.forEach(item->{
                            try {
                                //保存房源信息
                                HouseAnjuke houseAnjuke = BeanUtil.mapToBean(item, HouseAnjuke.class, true);
                                houseAnjukeService.save(houseAnjuke);
                                houseAnjukeRepository.save(houseAnjuke);
                                //增量同步ES
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        });
                    }
                })
                .addPipeline(new FilePipeline("/home/webmagic/"))
                .thread(1)
                .run();
        return AjaxResult.success();
    }

    /**
     * Es全量同步
     * @return
     */
    @GetMapping("sync")
    public AjaxResult sync() {
        //计算总页数
        int pageSize = 1000;
        long totalRecord = houseAnjukeService.count();
        long totalPage = (totalRecord  +  pageSize  - 1) / pageSize;
        //分页查询数据
        for (int pageNum = 1; pageNum <= totalPage; pageNum++) {
            //查询数据
            PageHelper.startPage(pageNum, pageSize);
            List<HouseAnjuke> list = houseAnjukeService.list();
            //启动多线程
            threadPoolTaskExecutor.execute(()->{
                //同步数据到Es
                houseAnjukeRepository.saveAll(list);
                //打印日志
                List<Long> houseIds = list.stream().map(item -> item.getId()).collect(Collectors.toList());
                log.info("HouseAnJuKe sync,dataIds:{}",houseIds);
            });
        }
        return AjaxResult.success("同步数据成功");
    }

    @GetMapping("all")
    public AjaxResult all() {
        List<HouseAnjuke> list = houseAnjukeService.list();
        houseAnjukeRepository.saveAll(list);
        return AjaxResult.success("同步数据成功");
    }


    /**
     * ES搜索列表
     * @param houseAnjuke
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("query")
    public AjaxResult query(HouseAnjuke houseAnjuke,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(pageNum-1, pageSize, Sort.Direction.DESC,"id");
        if (houseAnjuke.getTitle() == null || houseAnjuke.getTitle().equals("")) {
            houseAnjuke.setTitle("*");
        }
        Page<HouseAnjuke> page = houseAnjukeRepository.findAllByTitleOrDetailItemOrDetailItemBotTagOrAndDetailItemTag(houseAnjuke.getTitle(),houseAnjuke.getTitle(),houseAnjuke.getTitle(),houseAnjuke.getTitle(), pageRequest);
        return AjaxResult.success(page);
    }

    @GetMapping("keyword")
    public AjaxResult keyword(String keyword,
                            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize) {
        if (StringUtils.isEmpty(keyword)) {
            keyword = "*";
        }
        QueryStringQueryBuilder queryStringQueryBuilder = QueryBuilders.queryStringQuery(keyword).field("title");
        NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(queryStringQueryBuilder).build();
        // 设置分页
        PageRequest pageRequest = PageRequest.of(pageNum-1, pageSize);
        searchQuery.setPageable(pageRequest);
        //设置高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder()
                .field("title")
                .preTags("<font color='red'>")
                .postTags("</font>");
        HighlightQuery highlightQuery = new HighlightQuery(highlightBuilder);
        searchQuery.setHighlightQuery(highlightQuery);
        //查询
        SearchHits<HouseAnjuke> search = elasticsearchRestTemplate.search(searchQuery, HouseAnjuke.class);
        return AjaxResult.success(search);
    }


}
